package main

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"strings"
)

var goVersion string

func main() {

	goVersion = os.Getenv("GOVERSION")

	http.HandleFunc("/hello", hello)
	http.HandleFunc("/healthz", healthz)

	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		fmt.Println("启动 http 8080 服务失败")
	}
}

func hello(w http.ResponseWriter, r *http.Request) {
	// 1. request 的header，写入respond header
	header := r.Header
	for key, value := range header {
		fmt.Printf(" %s -> %s \n", key, value)

		w.Header().Set(key, strings.Join(value, " "))
	}

	// 2. 读取 version，写入header
	w.Header().Set("goVersion", goVersion)

	// 3. 标准输出 ip
	fmt.Printf("客户端ip：%s\n", r.RemoteAddr)

	// 3. TODO 没找到响应码

	io.WriteString(w, "hello")
}

// 4. 访问healthz，返回200
func healthz(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(200)
	io.WriteString(w, "200")
}
